﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
/// <summary>
/// Summary description for DatabaseInteraction
/// </summary>
public class DatabaseInteraction
{
    private static SqlConnection connect;
    private static SqlCommand command;
    

    public static SqlConnection GetConnection()
    {
        string connectStr = WebConfigurationManager.ConnectionStrings["northwindConnect"].ConnectionString;
        try
        {
            connect = new SqlConnection(connectStr);
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            return null;
        }
        return connect;
    }

    public static DataTable executeTable(string query, SqlParameter[] sqlParameter) {
        DataTable table = new DataTable();
        try
        {
            using (connect = GetConnection()) {
                connect.Open();
                using(command = new SqlCommand(query,connect)){
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddRange(sqlParameter);
                    
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            return null;
        }
        finally {
            if (connect != null) {
                connect.Close();
            }
        }

    }
}